<?php   
    class Member extends MY_Controller {
    
        function _init() {
            $this->load->library('auth');
            $this->load->library('form_validation', '', 'vform');
        }
        
        
        function login() {    
            $view_data = array(
                            'LOGIN_MESSAGE' => ''
                        );                    
            if($this->input->post('login_form_submit') != FALSE) {
            
                $this->vform->set_rules('username', 'Логин', 'required|trim');
                $this->vform->set_rules('password', 'Пароль', 'required|md5');
                                
                if($this->vform->run() == FALSE) {                    
                    $this->layout->load('login', $view_data);
                    return;                    
                } else {
                    $cond = array(
                        'username' => $this->input->post('username'),
                        'password' => $this->input->post('password'),
                    );
                        
                    if($this->auth->login($cond)) {
                        redirect('', 'refresh');
                    } else {
                        $view_data = array(
                            'LOGIN_MESSAGE' => 'Неверно введены логин или пароль, либо Ваша запись не активна.'
                        );
                    }
                }                                
            }
            
            $this->layout->load('login', $view_data);
        }
        
        function logout() {             
            $this->auth->logout();
            redirect('', 'refresh');
            $this->layout->load('logout');            
        }
        
        function register() {
            if($this->input->post('register_form_submit') != FALSE) {
                $this->load->library('form_validation', '', 'vform');
                $this->vform->set_rules('username', 'Логин', 'required|trim|callback_username_check');
                $this->vform->set_rules('password', 'Пароль', 'required|md5');
                $this->vform->set_rules('email', 'Электропочта', 'required|trim|valid_email');
                
                if($this->vform->run() == FALSE) {
                    $this->layout->load('register');
                    return;        
                } else {
                    $insert = array(
                        'active' => 0,
                        'username' => $this->input->post('username'),
                        'password' => $this->input->post('password'),
                        'email' => $this->input->post('email'),
                        'orgname' => $this->input->post('orgname'),
                        'address' => $this->input->post('address'),
                        'city' => $this->input->post('city'),
                        'phone' => $this->input->post('phone'),
                        'role_id' => 2,
                    );
                    $this->db->insert('users', $insert);
                    return $this->layout->load('register_ok');
                }
            }
            $this->layout->load('register');
        }
        
        function username_check($username) {
            $q = $this->db->where('username', $username)->get('users');
            if($q->num_rows() == 0) {
                return TRUE;
            } else {
                $this->vform->set_message('username_check', 'Пользователь '.$username.' уже зарегистрирован в системе.');
                return FALSE;
            }            
        }
        
        function orders() {            
            //$this->load->vars($view_data);
            $orders = $this->db->select('id, ports, budget, status, order_date')            
                ->where('client_id', $this->system->getUserId())
                ->get('orders_montag')->result_array();
            foreach($orders as &$order) {
                $order['order_date'] = date('d-m-Y H:i', $order['order_date']);
                $order['status'] = ($order['status'] == 0) ? 'Не утверждена' : 'Утверждена'; 
            }            
            if($this->input->post('order_form_submit') != FALSE) {
                $insert = array(
                    'status' => 0,
                    'order_date' => time(),
                    'budget' => $this->input->post('budget'),
                    'description' => $this->input->post('description'),
                    'ports' => $this->input->post('ports'),
                    'dop' => $this->input->post('dop'),
                    'client_id' => $this->system->getUserId(),
                );
                $this->db->insert('orders_montag', $insert);
                redirect('member/orders', 'refresh');
            }
            $this->load->library('Table');
            $this->table->set_template(array ( 'table_open'  => '<table width="100%">'));
            $this->table->set_heading('ID', 'Портов', 'Бюджет', 'Статус', 'Дата');
            $view_data = array(
                'ORDERS' => $this->table->generate($orders),
            );
            $this->load->vars($view_data);
            $this->layout->load('orders');
        }
        
        function addtocart() {
            //@toto - добавление в корзину
        }
    }
?>